{# Copying and distribution of this file, with or without modification,
   are permitted in any medium without royalty provided the copyright notice and this notice are preserved.
   This file is offered as-is, without any warranty.
#}
{% extends "include/base.html.twig" %}

{% block styles %}
  {{ parent() }}
  <link rel='stylesheet' href='css/access-token.css'>
{% endblock %}

{% block content %}
  <form name="{{ formName }}" method="POST">
  <input type="hidden" name="user_pk" value="{{ userId }}"/>
  {%  if isSessionAdmin %}
    {{ "Select the user to edit"|trans }}:
    {% import "include/macros.html.twig" as macro %}
    {{ macro.select('userid', allUsers, 'userid', userId, ' class="ui-render-select2" onchange="RefreshPage(this.value);"') }}
  {% endif %}

  <table style="border:1px solid black; border-collapse: collapse;" width="100%">
    <tr class="classic">
      <th width="25%">{{ "Username."|trans }}</th>
      <td><input type="text" value="{{ userName|e }}" name="user_name" size="20" {%- if not isSessionAdmin or userDescReadOnly == "true" -%}readonly="true"{%- endif -%}></td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "Description (name, contact, or other information)."|trans }} {{ "This may be blank."|trans }}</th>
      <td><input type="text" value="{{ userDescription|e }}" name="user_desc" size="60" {%- if userDescReadOnly == "true" -%}readonly="true"{%- endif -%}></td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "Email address."|trans }} {{ "This may be blank."|trans }}</th>
      <td><input type="text" value="{{ userEMail|e }}" name="user_email" size="60" {%- if userDescReadOnly == "true" -%}readonly="true"{%- endif -%}></td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "E-mail notification on job completion"|trans }}</th>
      <td><input type="checkbox" name="email_notify" {% if eMailNotification %}checked{% endif %}></td>
    </tr>
    {% if isSessionAdmin %}
      <tr class="classic">
        <th width="25%">{{ "Select the user's access level."|trans }}</th>
        <td>{{ macro.select('user_perm', allAccessLevels, 'user_perm', accessLevel) }}</td>
      </tr>
      <tr class="classic">
        <th width="25%">{{ "Select the user's status."|trans }} </th>
        {% if userStatus == "anonymized" %}          
          <td><select disabled name="disabled" }>
                <option value="anonymized">Anonymized</option>
              </select>
          </td>           
        {% else %}
          <td> {{ macro.select('user_status', allUserStatuses, 'user_status', userStatus) }}</td>
        {% endif %}
      </tr>
      <tr class="classic">
        <th width="25%">{{ "Select the user's top-level folder. Access is restricted to this folder."|trans }}</th>
        <td><select name="root_folder_fk" class="ui-render-select2">
          {{ folderListOption }}
        </select></td>
      </tr>
    {% endif %}
      <tr class="classic">
        <th width="25%">{{ "Select the user's default folder. Root for Upload and Browse will be this folder."|trans }}</th>
        <td><select name="default_folder_fk" class="ui-render-select2">
          {{ folderListOption2 }}
        </select></td>
      </tr>
    {% if isSessionAdmin %}
      {% if passwordPolicy is empty %}
      <tr class="classic">
        <th width="25%">{{ "Require no password."|trans }}</th>
        <td><input type="checkbox" name="_blank_pass" id="blank_pass" {% if isBlankPassword %}checked="checked" disabled="disabled"{% endif %}/></td>
      </tr>
      {% endif %}
    {% endif %}

    <tr class="classic">
      <th width="25%">{{ "Password."|trans }}</th>
      <td><input type="password" name="_pass1" size="20" id="passcheck"/>{% if passwordPolicy is not empty %}
      <br /><span class="passPolicy">{{ passwordPolicy }}</span>
      {% endif %}</td>
    </tr>
    <tr class="classic">
      <th width='25%'>{{ "Re-enter password."|trans }}</th>
      <td><input type="password" name="_pass2" size="20" id="pass2"/></td>
    </tr>
    <tr class="classic">
      <th width='25%'>{{ ('Default visibility') |trans }}</th>
      <td>
        <input type="radio" name="public" value="private" {% if uploadVisibility == "private" %} checked="checked" {% endif %}/> {{ 'Visible only for active group'| trans }}<img src="images/info_16.png" title="{{ 'which is the currently selected group'|trans }}" alt="" class="info-bullet"/><br/>
        <input type="radio" name="public" value="protected" {% if uploadVisibility == "protected" %} checked="checked" {% endif %}/> {{ 'Visible for all groups'| trans }}<img src="images/info_16.png" title="{{ 'which are accessible by you now'|trans }}" alt="" class="info-bullet"/><br/>
        <input type="radio" name="public" value="public" {% if uploadVisibility == "public" %} checked="checked" {% endif %}/> {{ 'Make Public'| trans }}<img src="images/info_16.png" title="{{ 'visible for all users'|trans }}" alt="" class="info-bullet"/><br/>
      </td>
    </tr>
    <tr class="classic">
      <th width='25%'>{{ 'Default agents selected when uploading data.'|trans }}</th>
      <td>{{ agentSelector }}</td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "Default bucket pool"| trans }}</th>
      <td>{{ bucketPool }}</td>
    </tr>
    {%  if isSessionAdmin %}
      <tr class="classic">
        <th width="25%">{{ "Select user's default group."|trans }}</th>
        <td><select name="default_group_fk" class="ui-render-select2">
          {{ defaultGroupOption }}
        </select></td>
      </tr>
    {% endif %}
  </table>
  <input type="submit" name="UpdateBtn" value="{{ 'Update Account'|trans }}"/>
  </form>
  <hr>
  <h3>{{ 'Rest API Tokens'|trans }}</h3>
  <p>
    {{ 'You can create/manage your tokens here for using FOSSology\'s REST API.'|trans }}
  </p>
  {% if newToken is not empty %}
  <h3>{{ 'New token'|trans }}</h3>
  <table style="border:1px solid black; border-collapse: collapse;" width="100%">
    <tr class="classic">
      <th style="width:20%">
        {{ 'New token for Bearer authentication'|trans }}
      </th>
      <td>
        <textarea readonly style="width:100%">{{ newToken }}</textarea>
      </td>
    </tr>
  </table>
  {% endif %}
  <h3>{{ 'Create a new token'|trans }}</h3>
  <form name="personal-access-token" method="POST">
    <table style="border:1px solid black; border-collapse: collapse;" width="100%">
      <tbody>
        <tr class="classic">
          <th width="20%">
            {{ 'Name'|trans }}
          </th>
          <td>
            <input type="text" name="pat_name" size="30" id="pat_name"
              placeholder="{{ 'Friendly name for new token'|trans }}" required="true" />
          </td>
        </tr>
        <tr class="classic">
          <th width="20%">
            {{ 'Expires at'|trans }}
          </th>
          <td>
            <input type="text" name="pat_expiry" id="pat_expiry"
              placeholder="YYYY-MM-DD" required="true" />
          </td>
        </tr>
        <tr class="classic">
          <th width="20%">
            {{ 'Access scope'|trans }}
          </th>
          <td>
            <table style="border:0px solid white;border-collapse:collapse" width="100%">
              <tbody>
                <tr>
                  <td>
                    <input type="radio" name="pat_scope" value="r"
                      id="pat_scope_r" checked="checked" />
                  </td>
                  <td style="width:100%">
                    <label class="font-weight:bold" for="pat_scope_r">
                      <div>
                        {{ 'Read only access'|trans }}
                      </div>
                      <div style="font-weight:lighter;color:#777">
                        {{ 'Limited only to "GET" calls'|trans }}
                      </div>
                    </label>
                  </td>
                </tr>
              {% if writeAccess %}
                <tr>
                  <td>
                    <input type="radio" name="pat_scope" value="w" id="pat_scope_w" />
                  </td>
                  <td style="width:100%">
                    <label class="font-weight:bold" for="pat_scope_w">
                      <div>
                        {{ 'Read/Write access'|trans }}
                      </div>
                      <div style="font-weight:lighter;color:#777">
                        {{ 'Required for calls other than "GET"'|trans }}
                      </div>
                    </label>
                  </td>
                </tr>
              {% endif %}
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    <input type="hidden" name="new_pat" value="true" />
    <input type="submit" value="{{ 'Create new token'|trans }}" />
  </form>
  {% if tokenList is not empty %}
    <hr>
    <h3>{{ 'Active access tokens '|trans }}({{ tokenList|length }})</h3>
    <table class="active-token-table">
      <thead>
        <tr>
          <th>
            {{ 'Token name'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Created on'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Expiry'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Scope'|trans }}
          </th>
          <th style="width:15%" colspan="2"></th>
        </tr>
      </thead>
      <tbody>
        {% for singleToken in tokenList %}
        <tr>
          <td>
            {{ singleToken.name }}
          </td>
          <td>
            {{ singleToken.created }}
          </td>
          <td>
            {{ singleToken.expire }}
          </td>
          <td>
            {%if singleToken.scope == 'r' %}
              {{ 'read only'|trans }}
            {% else %}
              {{ 'read/write'|trans }}
            {% endif %}
          </td>
          <td>
            <input type="button" class="pat-action pat-reveal" value="Reveal"
              id="reveal-{{ singleToken.id }}" />
          </td>
          <td>
            <input type="button" class="pat-action pat-revoke" value="Revoke"
              id="revoke-{{ singleToken.id }}" />
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}
  {% if expiredTokenList is not empty %}
    <hr>
    <h3>{{ 'Expired access tokens '|trans }}({{ expiredTokenList|length }})</h3>
    <table class="expired-token-table">
      <thead>
        <tr>
          <th>
            {{ 'Token name'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Created on'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Expired'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Scope'|trans }}
          </th>
          <th style="width:15%"></th>
        </tr>
      </thead>
      <tbody>
        {% for singleToken in expiredTokenList %}
        <tr>
          <td>
            {{ singleToken.name }}
          </td>
          <td>
            {{ singleToken.created }}
          </td>
          <td>
            {{ singleToken.expire }}
          </td>
          <td>
            {%if singleToken.scope == 'r' %}
              {{ 'read only'|trans }}
            {% else %}
              {{ 'read/write'|trans }}
            {% endif %}
          </td>
          <td>
            <input type="button" class="pat-action pat-reveal" value="Reveal"
              id="reveal-{{ singleToken.id }}" />
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}
  <br /><br /><br />
{% endblock %}

{% block foot %}
  {{ parent() }}
  <script language="javascript">
    function RefreshPage(val) {
      var uri = '{{ refreshUri }}' + '&newuser=' + val ;
      window.location.assign(uri);
    }

    {% if isBlankPassword %}
    $(document).ready(function(){
      $("#passcheck").keyup(function (){
        var isBlank = ($(this).val()==='');
        $("#blank_pass").prop("checked", isBlank);
      });
    });
    {% endif %}

    $(function() {
      $("#pat_expiry").datepicker({
        minDate : 0,
        maxDate : "+{{ maxTokenDate }}D",
        dateFormat : "yy-mm-dd",
        showOtherMonths : true,
        selectOtherMonths : true,
        showButtonPanel : true
      });
    });
  </script>
  <script src="scripts/user-edit.js" type="text/javascript"></script>
  <script type="text/javascript">{% include "password-policy-check.js.twig" %}</script>
{% endblock %}
